//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OneFactorAffineModel.h"
using namespace Cephei::QL::Models::Shortrate;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Models/ShortRateModel.h>
#include <gen/QL/Models/AffineModel.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (boost::shared_ptr<QuantLib::OneFactorAffineModel>& childNative, Object^ owner) : CAffineModel(COneFactorAffineModel::typeid)
{
#ifdef HANDLE
	_phOneFactorAffineModel = NULL;
#endif
	_ppOneFactorAffineModel = &childNative;
    _ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
}
Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (QuantLib::OneFactorAffineModel& childNative, Object^ owner) : CAffineModel(COneFactorAffineModel::typeid)
{
#ifdef HANDLE
	_phOneFactorAffineModel = NULL;
#endif
	_ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (&childNative);
    _ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
    _OneFactorAffineModelOwner = owner;
    _AffineModelOwner = owner;
}

Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (COneFactorAffineModel^ copy) : CAffineModel(COneFactorAffineModel::typeid)
{
#ifdef HANDLE
	_phOneFactorAffineModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (copy->GetShared());
        _ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
    }
}
Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (System::Type^ t) : CAffineModel(COneFactorAffineModel::typeid)
{
#ifdef HANDLE
	_phOneFactorAffineModel = NULL;
#endif
	if (!t->IsSubclassOf(COneFactorAffineModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (QuantLib::Handle<QuantLib::OneFactorAffineModel>& childNative, Object^ owner)  : CAffineModel(COneFactorAffineModel::typeid)
{
	_phOneFactorAffineModel = &childNative;
	_ppOneFactorAffineModel = &static_cast<boost::shared_ptr<QuantLib::OneFactorAffineModel>>(childNative.currentLink());
    _ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
    _OneFactorAffineModelOwner = owner;
}
Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (QuantLib::Handle<QuantLib::OneFactorAffineModel> childNative)  : CAffineModel(COneFactorAffineModel::typeid)
{
	_phOneFactorAffineModel = &childNative;
	_ppOneFactorAffineModel = &static_cast<boost::shared_ptr<QuantLib::OneFactorAffineModel>>(childNative.currentLink());
    _ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Shortrate::COneFactorAffineModel::COneFactorAffineModel (QuantLib::OneFactorAffineModel childNative)  : CAffineModel(COneFactorAffineModel::typeid)
{
#ifdef HANDLE
	_phOneFactorAffineModel = NULL;
#endif
	_ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (new QuantLib::OneFactorAffineModel (childNative));
    _ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
}
#endif

Cephei::QL::Models::Shortrate::COneFactorAffineModel::~COneFactorAffineModel ()
{
    if (_ppOneFactorAffineModel != NULL)
    {
	    delete _ppOneFactorAffineModel;
        _ppOneFactorAffineModel = NULL;
    }
}
Cephei::QL::Models::Shortrate::COneFactorAffineModel::!COneFactorAffineModel ()
{
    if (_ppOneFactorAffineModel != NULL)
    {
	    delete _ppOneFactorAffineModel;
    }
}
QuantLib::OneFactorAffineModel& Cephei::QL::Models::Shortrate::COneFactorAffineModel::GetReference ()
{
    if (_ppOneFactorAffineModel == NULL) throw gcnew NativeNullException ();
	return **_ppOneFactorAffineModel;
}
boost::shared_ptr<QuantLib::OneFactorAffineModel>& Cephei::QL::Models::Shortrate::COneFactorAffineModel::GetShared ()
{
    if (_ppOneFactorAffineModel == NULL) throw gcnew NativeNullException ();
	return *_ppOneFactorAffineModel;
}
QuantLib::OneFactorAffineModel* Cephei::QL::Models::Shortrate::COneFactorAffineModel::GetPointer ()
{
    if (_ppOneFactorAffineModel == NULL) throw gcnew NativeNullException ();
	return &**_ppOneFactorAffineModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OneFactorAffineModel>& Cephei::QL::Models::Shortrate::COneFactorAffineModel::GetHandle ()
{
	if (_phOneFactorAffineModel == NULL)
	{
		_phOneFactorAffineModel = new Handle<QuantLib::OneFactorAffineModel> (*_ppOneFactorAffineModel);
	}
	return *_phOneFactorAffineModel;
}
#endif
bool Cephei::QL::Models::Shortrate::COneFactorAffineModel::HasNative () 
{
	return (_ppOneFactorAffineModel != NULL);
}

Double Cephei::QL::Models::Shortrate::COneFactorAffineModel::Discount (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppOneFactorAffineModel)->discount ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Shortrate::COneFactorAffineModel::DiscountBond (Double now, Double maturity, Double rate)
{
    try
    {
        QuantLib::Time _now = (QuantLib::Time)ValueHelper::Convert (now);
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorAffineModel)->discountBond ( _now,  _maturity,  _rate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Shortrate::COneFactorAffineModel::DiscountBond (Double now, Double maturity, Cephei::QL::Math::IArray^ factors)
{
    CArray^ _Cfactors;
    try
    {
        QuantLib::Time _now = (QuantLib::Time)ValueHelper::Convert (now);
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        _Cfactors = safe_cast<CArray^> (factors);
        _Cfactors->Lock();
        QuantLib::Array _factors = static_cast<QuantLib::Array> (_Cfactors->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorAffineModel)->discountBond ( _now,  _maturity,  _factors );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cfactors != nullptr) _Cfactors->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

